package com.cxyzy.demo;

import android.animation.Animator;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout layout = findViewById(R.id.topLayout);

        for (Techniques techniques : Techniques.values()) {
            View view = createViewAndPlayAnimation(techniques);
            layout.addView(view);
        }
    }

    private View createViewAndPlayAnimation(Techniques techniques) {
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setPadding(0, 300, 0, 300);
        TextView titleTextView = new TextView(this);
        titleTextView.setText(techniques.name());
        titleTextView.setPadding(10, 0, 50, 0);
        TextView animateTextView = new TextView(this);
        animateTextView.setText("view动画测试");
        animateTextView.setBackgroundColor(Color.LTGRAY);
        linearLayout.addView(titleTextView);
        linearLayout.addView(animateTextView);
        playAnimation(animateTextView, techniques);
        return linearLayout;
    }


    /**
     * 执行动画
     * 下面样例中duration和repeat参数请根据实际修改；
     * onEnd是动画结束后的回调，如果不需要则去掉该函数
     * @param view 将执行动画的view
     * @param techniques 动画类型，详见Techniques类中枚举定义
     */
    private void playAnimation(View view, Techniques techniques) {
        YoYo.with(techniques)
                .duration(2000)
                .repeat(10000)
                .onEnd(new YoYo.AnimatorCallback() {
                    @Override
                    public void call(Animator animator) {
                        Toast.makeText(MainActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
                    }
                })
                .playOn(view);
    }
}
